package com.sf.dlb.controller;


import com.sf.dlb.bean.BzyNewsDataBean;
import com.sf.dlb.bean.RequestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.service.BzyExportRecodService;
import com.sf.dlb.service.NewListService;
import com.sf.dlb.service.ScheduledTaskService;
import com.sf.dlb.service.VideoListService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import com.sf.dlb.vo.TastGroupVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 新闻列表平台
 *
 * Created by jing.tan on 2018/5/21.
 */
@RestController
@RequestMapping(value="/v1/bzy")
public class CmsNewListController {
    private static Logger logger = LoggerFactory.getLogger(CmsNewListController.class);

    @Autowired
    public NewListService newListService;
    @Autowired
    public VideoListService videoListService;
    @Autowired
    public BzyExportRecodService bzyExportRecodService;
    @Autowired
    public ScheduledTaskService scheduledTaskService;
    /**
     * 手动执行定时任务
     * @return
     */
    @RequestMapping(value = "scheduledTask", method = RequestMethod.POST)
    @ResponseBody
    public Object scheduledTask(@RequestBody RequestBean requestBean) {
        if(null==requestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        scheduledTaskService.scheduledTask();
        return ResultVOUtil.success();
    }
    /**
     * 手动获取八爪鱼新闻数据
     * @return
     */
    @RequestMapping(value = "newList", method = RequestMethod.POST)
    @ResponseBody
    public Object newList(@RequestBody RequestBean requestBean) {
        logger.info("newList requestJson："+ GsonUtils.getInstance().toJson(requestBean));
        if(StringUtils.isNotBlank(requestBean.getTaskId())){
            //执行指定任务
            newListService.getNewList(requestBean.getTaskId());
        }
        return ResultVOUtil.success();
    }
    /**
     * 手动获取八爪鱼视频数据
     * @return
     */
    @RequestMapping(value = "videoList", method = RequestMethod.POST)
    @ResponseBody
    public Object videoList(@RequestBody RequestBean requestBean) {
        logger.info("videoList requestJson："+ GsonUtils.getInstance().toJson(requestBean));
        if(StringUtils.isNotBlank(requestBean.getTaskId())){
            //执行指定任务
            videoListService.getVideoList(requestBean.getTaskId());
        }
        return ResultVOUtil.success();
    }
    /**
     * 手动获取八爪鱼的任务和任务组
     * @return
     */
    @RequestMapping(value = "getBzyTask", method = RequestMethod.POST)
    @ResponseBody
    public Object getBzyTask(@RequestBody RequestBean requestBean) {
        if(null==requestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        if(StringUtils.isBlank(requestBean.getUserName())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户名不能为空");
        }
        logger.info("getBzyTask requestJson："+ GsonUtils.getInstance().toJson(requestBean));
        ResultVO resultVO= bzyExportRecodService.getTask(requestBean);
        logger.info("getBzyTask response："+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 手动增加任务和任务组
     * @return
     */
    @RequestMapping(value = "addBzyTask", method = RequestMethod.POST)
    @ResponseBody
    public Object addBzyTask(@RequestBody TastGroupVO requestBean) {
        if(null==requestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("addBzyTask request Json "+ GsonUtils.getInstance().toJson(requestBean));
        ResultVO resultVO= bzyExportRecodService.addBzyTask(requestBean);
        logger.info("addBzyTask response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

    /**
     * 手动生成数据
     * @return
     */
    @RequestMapping(value = "generateData", method = RequestMethod.POST)
    @ResponseBody
    public Object generateData(@RequestBody BzyNewsDataBean requestBean) {
        if(null==requestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        ResultVO resultVO= newListService.generateData(requestBean.getData());
        logger.info("generateData response："+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
